import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: '/login',
    component: () => import('@/views/Login/')
  },
  {
    path: '/home/',
    name: '/home',
    component: () => import('@/views/Home/'),
    redirect: '/main',
    children: [
      {
        path: '/main',
        name: '/main',
        component: () => import('@/views/Main/')
      },
      {
        path: '/user',
        name: '/user',
        component: () => import('@/views/User/')
      },
      {
        path: '/jurisdiction',
        name: '/jurisdiction',
        component: () => import('@/views/Jurisdiction/')
      },
      {
        path: '/power',
        name: '/power',
        component: () => import('@/views/Power/')
      },
      {
        path: '/goods',
        name: '/goods',
        component: () => import('@/views/Goods/')
      },
      {
        path: '/params',
        name: '/params',
        component: () => import('@/views/Params/')
      },
      {
        path: '/list',
        name: '/list',
        component: () => import('@/views/List/')
      },
      {
        path: '/goods/add',
        name: '/goods/add',
        component: () => import('@/views/List/Add.vue')
      },
      {
        path: '/orders',
        name: '/orders',
        component: () => import('@/views/Orders/')
      },
      {
        path: '/reports',
        name: '/reports',
        component: () => import('@/views/Reports/')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 路由守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从那个路径跳转而来
  // next 是一个函数,表示放行
  // next() 放行,next('/login') 强制跳转

  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) {
    next('/login')
  } else {
    next()
  }
})
export default router
